// pages/shopCar/shopCar.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopCar: [
      {
          "sid": 10002,
          "id": 10017,
          "category": "饮料",
          "name": "咖啡",
          "description": "香浓的现磨咖啡，提神醒脑",
          "price": 20,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.d3e0603de2bfc9b811df55820c7a1a32?rik=DUR1qa4femur6w&riu=http%3a%2f%2fseopic.699pic.com%2fphoto%2f50073%2f8464.jpg_wh1200.jpg&ehk=El%2fvrDPlNonn%2f7EQsDx2tDZHuHQsYwfvUX5cv7ifjU0%3d&risl=&pid=ImgRaw&r=0",
          "num": 2
      },
      {
          "sid": 10001,
          "id": 10016,
          "category": "饮料",
          "name": "鲜榨橙汁",
          "description": "新鲜的橙汁，维生素丰富",
          "price": 15,
          "image_url": "https://tse3-mm.cn.bing.net/th/id/OIP-C.muk6x90XSjrlHCPuAxrLXQHaIA?rs=1&pid=ImgDetMain",
          "num": 1
      },
      {
          "sid": 10005,
          "id": 10015,
          "category": "饮料",
          "name": "柠檬冰茶",
          "description": "清爽的柠檬冰茶，解渴佳品",
          "price": 10,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.afd3567e997963984eeb432f8b96b894?rik=Tz%2f6iE4eUmPfjg&riu=http%3a%2f%2fpic.qianye88.com%2f4kcc0c32a3083-23d4-3fdd-848b-5ce4363657bc.jpeg&ehk=ssdUrgWNPqxti3b1dlTiFj5fDFPAYVRM6%2b3jXMj6HFo%3d&risl=&pid=ImgRaw&r=0",
          "num": 2
      },
      {
          "sid": 10003,
          "id": 10014,
          "category": "甜点",
          "name": "红豆沙",
          "description": "传统的红豆沙，甜而不腻",
          "price": 12,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.2bc0f68c207b3803e6a5311e73584a3e?rik=Qog6ZwS1rnvT2g&riu=http%3a%2f%2fi2.chuimg.com%2f6e1f2932890a11e6a9a10242ac110002_800w_800h.jpg%3fimageView2%2f2%2fw%2f660%2finterlace%2f1%2fq%2f90&ehk=uvSQV62SmvzzRHnBXFCag4xbP15UgylY%2buvLOpd3dMo%3d&risl=&pid=ImgRaw&r=0",
          "num": 1
      },
      {
          "sid": 10004,
          "id": 10013,
          "category": "甜点",
          "name": "芒果布丁",
          "description": "香甜的芒果布丁，口感细腻",
          "price": 15,
          "image_url": "https://tse2-mm.cn.bing.net/th/id/OIP-C.ez25r2k_DXVInRe4F7kNtAHaFy?rs=1&pid=ImgDetMain",
          "num": 1
      },
      {
          "sid": 10008,
          "id": 10011,
          "category": "主食",
          "name": "扬州炒饭",
          "description": "经典的扬州炒饭，内有虾仁、鸡蛋等",
          "price": 20,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.e638c892aeabcbf7e893bac560a6acac?rik=9QvszcMMSq2jFw&riu=http%3a%2f%2fi2.chuimg.com%2f3b87d54c89b811e6a9a10242ac110002_1280w_1280h.jpg%3fimageView2%2f2%2fw%2f660%2finterlace%2f1%2fq%2f90&ehk=1Mvempk5YqNFJS00Esl3MaNQaF2kng30fvrvTf%2b7kpk%3d&risl=&pid=ImgRaw&r=0",
          "num": 2
      },
      {
          "sid": 10007,
          "id": 10010,
          "category": "素菜",
          "name": "香菇青菜",
          "description": "鲜香的香菇，配以翠绿的青菜",
          "price": 25,
          "image_url": "https://cp1.douguo.com/upload/caiku/5/b/8/yuan_5be454733791f1bd0139859ae9146d78.jpg",
          "num": 1
      },
      {
          "sid": 10009,
          "id": 10004,
          "category": "主菜",
          "name": "红烧牛肉",
          "description": "鲜嫩的牛肉块，搭配香浓的红烧酱汁",
          "price": 60,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.bc19683715060254450e6143f83512b1?rik=FUws3LuqAcFlTA&riu=http%3a%2f%2fi2.chuimg.com%2feea3d6a1749445c0999e0c7369a027e9_3024w_3024h.jpg%3fimageView2%2f2%2fw%2f660%2finterlace%2f1%2fq%2f90&ehk=XQ6YCJGXTPS7pq%2fpVWOJ%2forYIyceax9U6hEJ90J0qlI%3d&risl=&pid=ImgRaw&r=0",
          "num": 2
      },
      {
          "sid": 10006,
          "id": 10001,
          "category": "前菜",
          "name": "凉拌黄瓜",
          "description": "清爽的凉拌黄瓜，配以蒜蓉和香油",
          "price": 15,
          "image_url": "https://ts1.cn.mm.bing.net/th/id/R-C.214466f9bb9fea5243c8b1c0f3a80f0f?rik=KWZwRK7NsoDd%2fA&riu=http%3a%2f%2fi3.meishichina.com%2fattachment%2frecipe%2f2017%2f07%2f04%2f20170704149916366743910121381.jpg%40!p800&ehk=AiMHzI0p4CLOmDODLvxUYLN1sogJknw23%2fet9Ey9y9M%3d&risl=&pid=ImgRaw&r=0",
          "num": 1
      }
  ],
    totalPrice: 0
  },
  onLoad() {
    this.getShopCar()
    // this.calculateTotalPrice()
  },
  getShopCar() {
    let that = this
    wx.request({
      url: "http://localhost:7000/menu-service/shopCarList",
      method: "GET",
      // success(res) {
      //   that.setData({
      //     shopCar: res.data
      //   })
      // }
    })
  },



  calculateTotalPrice() {
    let total = 0;
    this.data.shopCar
    for (let i = 0; i < this.data.shopCar.length; i++) {
      console.log(2212312313)
      total += this.data.shopCar[i].price * this.data.shopCar[i].num;
    }
    this.setData({
      totalPrice: total
    });
  },

  numMinus: function (e) {
    // 减少商品数量的逻辑
    var i = e.target.dataset.index
    if (this.data.shopCar[i].num > 0) {
      this.setData({
        ['shopCar[' + i + '].num']: this.data.shopCar[i].num - 1
      })
    }
    this.calculateTotalPrice()
  },
  numAdd: function (e) {
    var i = e.target.dataset.index
    // 增加商品数量的逻辑
    this.setData({
      ['shopCar[' + i + '].num']: this.data.shopCar[i].num + 1
    })
    this.calculateTotalPrice()
  }
})